
#include <string.h>
#include "BSP_Delay.h"
#include "BSP_Relay.h"
#include "Timer.h"

#include "Indecator.h"


static void Indecator_Relay_Set(uint32_t id, uint32_t state){
	BSP_Relay_Set(state ? 1 : 0);
}

	
static Indecator_T doorIndecator = {
	.obj = {
		.objId = 0,
		.action = Indecator_Relay_Set,
	},
};

static const IndecatorSetting_T doorPermanentOff ={
		.states[State_Off] = {0, 0xFFFF},	//唯一有效状态
		.states[State_On] = {1, 0},	//无效
		.duration = 0,	//永久有效
		.current = State_Off,
};
		
static const IndecatorSetting_T doorPermanentOn ={
		.states[State_Off] = {0, 0},		//无效
		.states[State_On] = {1, 0xFFFF},	//唯一有效状态
		.duration = 0,	//永久有效
		.current = State_On
};
		
void Door_Init(void){	
	BSP_Relay_Set(0);
	Indecator_Init();
	Indecator_Set(&doorIndecator, Setting_Permanent, &doorPermanentOff, 0);
	Indecator_Register(&doorIndecator);
}


void Door_TempSwitch(uint32_t ms){

	if(ms < 10){
		return;
	}
	const IndecatorSetting_T * doorPermanent = &doorIndecator.settings[Setting_Permanent];
	IndecatorSetting_T temp = {
		.states[State_Off] = {0, ms/10},		//无效
		.states[State_On] = {1, ms/10},	//唯一有效状态
		.duration = ms/10,	
		.current = doorPermanent->current == State_Off ? State_On : State_Off,
	};
	
	Indecator_Set(&doorIndecator, Setting_Temp, 0, &temp);
}

void Door_SetPermanent(uint8_t state){
	const IndecatorSetting_T * doorPermanent = state ? &doorPermanentOn:&doorPermanentOff;
	Indecator_Set(&doorIndecator, Setting_Permanent, doorPermanent, 0);
}


int Door_State(void){
	return BSP_Relay_State();
}


